#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
using namespace std;
string serverip="0.0.0.0";
uint16_t serverport=8080;
const int sz=1024;
int main(int argc,char*argv[])
{

if(argc!=3)
{
  cout<<"please: "<<"./client ip_ port"<<endl;
  exit(1);
}

string serverip=argv[1];
int16_t serverport=stoi(argv[2]);

struct sockaddr_in server;
bzero(&server,sizeof(server));
//socklen_t len=sizeof(server);
server.sin_family=AF_INET;
server.sin_port=htons(serverport);
server.sin_addr.s_addr=inet_addr(serverip.c_str());
socklen_t len=sizeof(server);


int sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
cout<<"创建套接字失败"<<endl;
return 1;


}
cout<<"创建套接字成功"<<endl;

string message;
char buffer[sz];
while(1)
{

cout<<"please enter@:";
getline(cin,message);
sendto(sockfd,message.c_str(),message.size(),0,(const sockaddr*)&server,len);
struct sockaddr_in temp;
socklen_t len=sizeof(temp);
ssize_t n=recvfrom(sockfd,buffer,1023,0,(sockaddr*)&temp,&len);
if(n>0)
{
 buffer[sz]=0;
 cout<<buffer<<endl;
}


}


close(sockfd);
return 0;


}
